//
//  GoodsDetailBottomView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/20.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "GoodsDetailBottomView.h"

@implementation GoodsDetailBottomView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self configure];
//    }
//    return self;
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self configure];
}

- (void)configure {
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"GoodsDetailBottomView" owner:self options:nil] firstObject];
    self.view.frame = self.bounds;
    [self addSubview:self.view];
    
    self.serviceButton.titleLabel.font = TextPingFangFontSize(10);
    [self.serviceButton setTitleColor:[UIColor gray66Color] forState:UIControlStateNormal];
    [self.serviceButton setTitle:@"客服" forState:UIControlStateNormal];
    
    self.collectButton.titleLabel.font = TextPingFangFontSize(10);
    
    self.cartButton.titleLabel.font = TextPingFangFontSize(10);
    [self.cartButton setTitleColor:[UIColor gray66Color] forState:UIControlStateNormal];
    [self.cartButton setTitle:@"购物车" forState:UIControlStateNormal];
    
    self.addCartButton.titleLabel.font = TextPingFangFontSize(14);
    [self.addCartButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.buyButton.titleLabel.font = TextPingFangFontSize(14);
    [self.buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.creditsButton.titleLabel.font = TextPingFangFontSize(14);
    [self.creditsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.addCartButton setTitle:@"加入购物车" forState:UIControlStateNormal];
    [self.addCartButton.titleLabel setNumberOfLines:0];
    [self.addCartButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.buyButton.titleLabel setNumberOfLines:0];
    [self.buyButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.creditsButton setTitle:@"立即兑换" forState:UIControlStateNormal];
    
    self.addCartBackView.backgroundColor = [UIColor yellowAddCartColor];
    self.buyBackView.backgroundColor = [UIColor redF2color];
    self.creditsBackView.backgroundColor = [UIColor redF2color];

    
    [self setCollect:NO];
}

- (void)setBottomValueWith:(GoodDetailModel *)model type:(GoodsDetailViewControllerType)type {
    [self setCollect:model.iscollection];
    switch (type) {
        case GoodsDetailViewControllerTypeForNormal:
        case GoodsDetailViewControllerTypeForAbroad:
        case GoodsDetailViewControllerTypeForFlashSale:
        {
            self.addCartBackView.hidden = NO;
            self.buyBackView.hidden = NO;
            self.creditsBackView.hidden = YES;
            
            self.serviceImageView.hidden = NO;
            self.serviceButton.hidden = NO;
            self.collectImageView.hidden = NO;
            self.collectButton.hidden = NO;
            self.cartImageView.hidden = NO;
            self.cartButton.hidden = NO;
            
            self.serviceButtonWidth.constant = 53;
            self.collectButtonWidth.constant = 53;
            self.cartButtonWidth.constant = 53;
        } break;
        case GoodsDetailViewControllerTypeForGroup: {
            self.addCartBackView.hidden = NO;
            self.buyBackView.hidden = NO;
            self.creditsBackView.hidden = YES;
            [self setAddCartButtonAttributeTitle:model.gprice];
            [self setBuyButtonAttributeTitle:model.pgprice];
        } break;
        case GoodsDetailViewControllerTypeForCredits: {
            self.addCartBackView.hidden = YES;
            self.buyBackView.hidden = YES;
            self.creditsBackView.hidden = NO;
        } break;
//        case GoodsDetailViewControllerTypeForFlashSale: {
//            self.addCartBackView.hidden = NO;
//            self.buyBackView.hidden = NO;
//            self.creditsBackView.hidden = YES;
//
//            self.serviceImageView.hidden = YES;
//            self.serviceButton.hidden = YES;
//            self.collectImageView.hidden = YES;
//            self.collectButton.hidden = YES;
//            self.cartImageView.hidden = YES;
//            self.cartButton.hidden = YES;
//
//            self.serviceButtonWidth.constant = 0;
//            self.collectButtonWidth.constant = 0;
//            self.cartButtonWidth.constant = 0;
//
//        } break;
        case GoodsDetailViewControllerTypeForNearby: {
            self.addCartBackView.hidden = NO;
            self.buyBackView.hidden = NO;
            self.creditsBackView.hidden = YES;
            
        } break;
            
        default:
            break;
    }
}

#pragma mark - Action

- (IBAction)serviceButtonClick:(id)sender {
    if (self.block) {
        self.block(0);
    }
}
- (IBAction)collecButtonClick:(id)sender {
    if (self.block) {
        self.block(1);
    }
}
- (IBAction)cartButtonClick:(id)sender {
    if (self.block) {
        self.block(2);
    }
}
- (IBAction)addCartButtonClick:(id)sender {
    if (self.block) {
        self.block(3);
    }
}
- (IBAction)buyButtonClick:(id)sender {
    if (self.block) {
        self.block(4);
    }
}
- (IBAction)creditsButtonClick:(id)sender {
    if (self.block) {
        self.block(5);
    }
}

#pragma mark - Setters
- (void)setCollect:(BOOL)isCollect {
    if (isCollect) {
        [self.collectButton setTitle:@"已收藏" forState:UIControlStateNormal];
        [self.collectButton setTitleColor:[UIColor redF2color] forState:UIControlStateNormal];
        self.collectImageView.image = [UIImage imageNamed:@"collected"];
    } else {
        [self.collectButton setTitle:@"收藏" forState:UIControlStateNormal];
        [self.collectButton setTitleColor:[UIColor gray66Color] forState:UIControlStateNormal];
        self.collectImageView.image = [UIImage imageNamed:@"collection_ash"];
    }
}

- (void)setAddCartButtonAttributeTitle:(NSString *)price {
    
    NSString *sourceStr = [NSString stringWithFormat:@"¥%@\n单独买",price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:sourceStr];
    
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(11)} range:NSMakeRange(0, sourceStr.length)];
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(13)} range:[sourceStr rangeOfString:@"单独买"]];
//    //行间距
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    paragraphStyle.lineSpacing = 0;
//    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
//    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, sourceStr.length)];
    
    [self.addCartButton setAttributedTitle:str forState:UIControlStateNormal];
}
- (void)setBuyButtonAttributeTitle:(NSString *)price {
    NSString *sourceStr = [NSString stringWithFormat:@"¥%@\n团购",price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:sourceStr];
    
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(11)} range:NSMakeRange(0, sourceStr.length)];
    [str setAttributes:@{NSFontAttributeName:TextPingFangFontSize(13)} range:[sourceStr rangeOfString:@"团购"]];
//    //行间距
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    paragraphStyle.lineSpacing = 0;
//    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
//    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, sourceStr.length)];
    
    [self.buyButton setAttributedTitle:str forState:UIControlStateNormal];
}
@end

